const UsersModels = require('../models/UsersModel')
const ResumeModels = require('../models/ResumeModel')
const BaseController = require('./BaseController')
const day = require('dayjs')

class ResumeController extends BaseController {
  constructor(args) {
    super(args)
  }

  /**查询简历详情*/
  static async detail(ctx) {
    console.log('ctx.request.query', ctx.request.query)
    const user_id = ctx.request.query.user_id
    try {
      if (!user_id) return ctx.fail('用户不存在')
      const res = await ResumeModels.findOne({ user_id })
      ctx.success(res)
    } catch (e) {
      ctx.fail(e.message)
    }
  }

  /**新增用户对应的简历信息*/
  static async add(ctx) {
    try {
      const user_id = ctx.request.body.user_id
      let data = ctx.request.body.resume_data
      if (data) {
        data.user_id = user_id
        data.create_time = day().format('YYYY-MM-DD HH:mm:ss')
        await ResumeModels.create(data)
        ctx.success()
      } else {
        ctx.fail('简历内容不能为空！')
      }
    } catch (e) {
      ctx.fail(e.message)
    }
  }

  /**更新简历信息*/
  static async update(ctx) {
    try {
      let data = ctx.request.body.resume_data
      if (data) {
        data.update_time = day().format('YYYY-MM-DD HH:mm:ss')
        const res = await ResumeModels.updateOne(
          {
            _id: ctx.request.body._id
          },
          data
        )
        ctx.success(res)
      } else {
        ctx.fail('简历内容不能为空！')
      }
    } catch (e) {
      ctx.fail(e.message)
    }
  }
}
module.exports = ResumeController
